import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'

// 包含所有用户都能操作的路由,也就是不需要权限就能操作的路由,即常量路由
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'dashboard' }
      }
    ]
  }
]

/*
  商品管理相关的路由,不是常量路由
  根据用户返回的权限数据信息决定是否注册这个路由,即异步路由
  异步路由: 需要根据权限数据来从这个路由数组当中过滤出用户所拥有的异步路由
*/
export const allAsyncRoutes = [
  {
    path: '/product',
    // 首先得显示一级路由组件
    component: Layout,
    name: 'Product',
    meta: { title: '商品管理', icon: 'el-icon-s-shop' },
    redirect: '/product/trademark/list',
    children: [
      {
        path: 'trademark/list',
        name: 'Trademark',
        meta: { title: '品牌管理' },
        component: () => import('@/views/product/trademark/List')
      },
      {
        path: 'attr/list',
        name: 'Attr',
        meta: { title: '属性管理' },
        component: () => import('@/views/product/attr/List')
      },
      {
        path: 'spu/list',
        name: 'Spu',
        meta: { title: 'SPU管理' },
        component: () => import('@/views/product/spu/List')
      },
      {
        path: 'sku/list',
        name: 'Sku',
        meta: { title: 'SKU管理' },
        component: () => import('@/views/product/sku/List')
      }
    ]
  },
  {
    name: 'Acl',
    path: '/acl',
    component: Layout,
    redirect: '/acl/user/list',
    meta: {
      title: '权限管理',
      icon: 'el-icon-lock'
    },
    children: [
      {
        name: 'User',
        path: 'user/list',
        component: () => import('@/views/acl/user/list'),
        meta: {
          title: '用户管理',
        },
      },
      {
        name: 'Role',
        path: 'role/list',
        component: () => import('@/views/acl/role/list'),
        meta: {
          title: '角色管理',
        },
      },
      {
        name: 'RoleAuth',
        path: 'role/auth/:id',
        component: () => import('@/views/acl/role/roleAuth'),
        meta: {
          activeMenu: '/acl/role/list',
          title: '角色授权',
        },
        hidden: true,
      },
      {
        name: 'Permission',
        path: 'permission/list',
        component: () => import('@/views/acl/permission/list'),
        meta: {
          title: '菜单管理',
        },
      },
    ]
  },
]

/*
  任意路由,用户随意输入的非法路由路径,全部都会转到 404 路由界面
  注册这个路由的时候,一定要放在最后面注册
*/
export const anyRoute = { path: '*', redirect: '/404', hidden: true }

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
